/*
 *  Copyright 2008-2011 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file memory.h
 *  \brief Classes for managing CUDA-typed memory.
 */

#pragma once

#include <thrust/detail/config.h>
#include <thrust/detail/pointer_base.h>
#include <thrust/detail/reference_base.h>
#include <thrust/detail/type_traits.h>
#include <ostream>

namespace thrust
{
namespace cuda
{

// forward declaration
template<typename T> class reference;

template<typename T>
  class pointer
    : public thrust::detail::pointer_base<
               thrust::cuda::pointer<T>,
               T,
               thrust::cuda::reference<T>,
               thrust::detail::cuda_device_space_tag
             >
{
  private:
    typedef thrust::detail::pointer_base<
      thrust::cuda::pointer<T>,
      T,
      thrust::cuda::reference<T>,
      thrust::detail::cuda_device_space_tag
    > super_t;

  public:
    // XXX doxygenate these
    
    // note that cuda::pointer's member functions need __host__ __device__
    // to interoperate with nvcc + thrust::detail::dereference

    __host__ __device__
    pointer() : super_t() {}

    template<typename OtherT>
    __host__ __device__
    explicit pointer(OtherT *ptr) : super_t(ptr) {}

    template<typename OtherT>
    __host__ __device__
    pointer(const pointer<OtherT> &other) : super_t(other) {}

    template<typename OtherT>
    __host__ __device__
    pointer &operator=(const pointer<OtherT> &other)
    {
      super_t::operator=(other);
      return *this;
    }
}; // end pointer


template<typename T>
  class reference
    : public thrust::detail::reference_base<
               thrust::cuda::reference<T>,
               T,
               thrust::cuda::pointer<T>
             >
{
  private:
    typedef thrust::detail::reference_base<
      thrust::cuda::reference<T>,
      T,
      thrust::cuda::pointer<T>
    > super_t;

  public:
    typedef typename super_t::value_type value_type;
    typedef typename super_t::pointer    pointer;

    template<typename OtherT>
    __host__ __device__
    reference(const reference<OtherT> &other,
              typename thrust::detail::enable_if_convertible<
                typename reference<OtherT>::pointer,
                pointer
              >::type * = 0)
      : super_t(other)
    {}

    __host__ __device__
    explicit reference(const pointer &ptr)
      : super_t(ptr)
    {}

    template<typename OtherT>
    reference &operator=(const reference<OtherT> &other);

    reference &operator=(const value_type &x);
};

template<typename T>
__host__ __device__
void swap(reference<T> &x, reference<T> &y);

inline pointer<void> malloc(std::size_t n);

inline void free(pointer<void> ptr);


template<typename T> class allocator;

template<>
  class allocator<void>
{
  public:
    typedef void                             value_type;
    typedef thrust::cuda::pointer<void>       pointer;
    typedef thrust::cuda::pointer<const void> const_pointer;
    typedef std::size_t                      size_type;
    typedef pointer::difference_type         difference_type;

    template<typename U>
      struct rebind
    {
      typedef allocator<U> other;
    }; // end rebind
}; // end allocator<void>

template<typename T>
  class allocator
{
  public:
    typedef T                                 value_type;
    typedef thrust::cuda::pointer<T>           pointer;
    typedef thrust::cuda::pointer<const T>     const_pointer;
    typedef thrust::cuda::reference<T>         reference;
    typedef thrust::cuda::reference<const T>   const_reference;
    typedef std::size_t                       size_type;
    typedef typename pointer::difference_type difference_type;

    template<typename U>
      struct rebind
    {
      typedef allocator<U> other;
    }; // end rebind

    __host__ __device__
    inline allocator();

    __host__ __device__
    inline allocator(const allocator &);

    template<typename U>
    __host__ __device__
    inline allocator(const allocator<U> &);

    __host__ __device__
    ~allocator();

    __host__ __device__
    pointer address(reference x) const;

    __host__ __device__
    const_pointer address(const_reference x) const;

    pointer allocate(size_type cnt, allocator<void>::const_pointer hint = allocator<void>::const_pointer());

    void deallocate(pointer p, size_type n);

    size_type max_size() const;
}; // end allocator

template<typename T1, typename T2>
__host__ __device__
bool operator==(const allocator<T1> &, const allocator<T2> &);

template<typename T1, typename T2>
__host__ __device__
bool operator!=(const allocator<T1> &, const allocator<T2> &);

} // end cuda

} // end thrust

#include <thrust/cuda/detail/memory.inl>

